//*********基础功能重新封装**************//
function apiGet(url, data, func) {

    // log("进入apiGet,正在获取数据")
    var header ={
        "token": "834fc75d3a36bbc5152211c045dbab3f"
        , "nowtime": "123456"
    }
    var at = getLocData("at");
    if (isTrue(at)) {
        header["access-token"] = at;
    }
    var auth_token = getLocData("auth_token");
    if (isTrue(auth_token)) {
        header["X-AUTH-TOKEN"] = auth_token;
    }
    console.log(header)

    $.ajax({
        url: baseApi + url,
        method: 'GET',
        returnAll: true,
        headers: header,
        dataType: "json",
        data: data,
        success: function (ret, err,t1) {
            console.log(ret)
            console.log(err)
            console.log(t1)
            var cookie = getCookie(ret.headers)
            // console.log(JSON.stringify(ret));
            // console.log(JSON.stringify(err.msg));
            // log(err.msg)
            // logJson(ret)
            if (typeof cookie == "string") {
                setLocData("cookie", cookie);
            }
            // func(ret)
            apiDealWithResult(ret, func,t1.status,t1)
        }
        ,error:function(a,b,c,d){

            console.log(a)
            console.log(b)
            console.log(c)
            console.log(d)
            apiDealWithResult(a.responseJSON, func,a.status,a)
        }
    });
}

function apiLoad(url, data, func) {
    showLoading()
    apiGet(url, data, function (res) {
        clearLoading()
        func(res)
    })
}


function apiLog(data) {
    $.ajax({
        url: loggerUrl,
        method: 'POST',
        returnAll: true,
        headers: {
            "Cookie": cookie()
        },
        dataType: "json",
        data: {values: {content: data}}
    }, function (ret, err) {
    });
}

function apiPost(url, data, func) {
    var header ={
        "token": "834fc75d3a36bbc5152211c045dbab3f"
        , "nowtime": "123456"
    }
    var at = getLocData("at");
    if (isTrue(at)) {
        header["access-token"] = at;
    }
    var auth_token = getLocData("auth_token");
    if (isTrue(auth_token)) {
        header["X-AUTH-TOKEN"] = auth_token;
    }
    console.log(header)
    $.ajax({
        url: baseApi + url,
        method: 'POST',
        returnAll: true,
        headers: header,
        dataType: "json",
        data: data
        ,
        success: function (ret, err) {
            console.log(ret)
            console.log(err)
            var cookie = getCookie(ret.headers)
            if (typeof cookie == "string") {
                setLocData("cookie", cookie);
            }
            apiDealWithResult(ret, func)
        }
    });
}

function apiUpload(url, path, func) {
    log("进入apiGet,正在获取数据")
    log(url)
    logJson(path)
    var files = {Filedata: path}
    $.ajax({
        url: baseApi + url,
        method: 'POST',
        returnAll: true,
        headers: {
            "Cookie": cookie()
        },
        dataType: "json",
        data: {files: files}
    }, function (ret, err) {
        logHtml(err.msg)
        var cookie = getCookie(ret.headers)
        if (typeof cookie == "string") {
            setLocData("cookie", cookie);
        }
        apiDealWithResult(ret, func)
    });
}

function getCookie(headers) {
    // var cookies = [];
    // for (var i in headers) {
    //   if (i == "Set-Cookie") {
    //       var arr = headers[i].split(";")
    //       cookies = arr[0];
    //   }
    // }
    // return cookies;
}

function cookie() {
    return getLocData("cookie")
}


function apiDealWithResult(res, callback,code,full) {

    console.log(full.getResponseHeader("access-token"))
    var at = full.getResponseHeader("access-token")
    setLocData("at",at)
    // console.log(full.getResponseHeader())

    callback(res,code)
}

function update() {
}
